package org.jctc.els.modules.biz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jctc.els.modules.biz.entity.projects.*;
import org.jctc.els.modules.biz.excel.ElsReportGouzhiJfysExcel;
import org.jctc.els.modules.biz.excel.ElsReportKaifaWwfysmxExcel;
import org.jctc.els.modules.biz.mapper.ElsProjectFileGuanLianMapper;
import org.jctc.els.modules.biz.mapper.ElsProjectReportExploitMapper;
import org.jctc.els.modules.biz.mapper.projects.ElsReportKaifaWwfysmxMapper;
import org.jctc.els.modules.biz.service.IElsProjectReportExploitService;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Service
public class ElsProjectReportExploitServiceImpl implements IElsProjectReportExploitService {

    @Autowired
    ElsProjectReportExploitMapper elsProjectReportExploitMapper;

    @Autowired
    ElsProjectFileGuanLianMapper elsProjectFileGuanLianMapper;

    @Autowired
    ElsReportKaifaWwfysmxMapper wwfysmxMapper;

    @Override
    public ElsProjectReportExploit getByProjectId(String projectId) {
        return elsProjectReportExploitMapper.selectOne(new QueryWrapper<ElsProjectReportExploit>().eq("project_id",projectId));
    }

    @Override
    public Integer save(ElsProjectReportExploit elsProjectReportExploit) {
        return elsProjectReportExploitMapper.insert(elsProjectReportExploit);
    }

    @Override
    public Integer update(ElsProjectReportExploit elsProjectReportExploit) {
        return elsProjectReportExploitMapper.updateById(elsProjectReportExploit);
    }

    @Override
    public ElsProjectFileGuanlian getFilePath(String id) {
        return elsProjectFileGuanLianMapper.selectOne(new QueryWrapper<ElsProjectFileGuanlian>().eq("id",id));
    }

    @Override
    public Integer saveGuanLianPath(ElsProjectFileGuanlian elsProjectFileGuanlian) {
        return elsProjectFileGuanLianMapper.insert(elsProjectFileGuanlian);
    }

    @Override
    public Integer updateGuanLianPath(ElsProjectFileGuanlian elsProjectFileGuanlian) {
        return elsProjectFileGuanLianMapper.updateById(elsProjectFileGuanlian);
    }

    @Override
    public ElsProjectReportExploit getProjectId(Long id) {
        return elsProjectReportExploitMapper.selectOne(new QueryWrapper<ElsProjectReportExploit>().eq("project_id",id));
    }

    @Override
    public R importPurchaseProjects(MultipartFile multipartFile, HttpServletRequest request, String gouZhiId) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<ElsReportKaifaWwfysmxExcel> data = ExcelUtil.read(multipartFile, ElsReportKaifaWwfysmxExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        List<ElsReportKaifaWwfysmx> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            ElsReportKaifaWwfysmxExcel wwfysmxExcel = data.get(i);
            String gznr = wwfysmxExcel.getGznr();
            if(StrUtil.isEmpty(gznr)){
                throw new RuntimeException("第"+(i+1)+"行工作内容不可为空！");
            }
            Integer sl = wwfysmxExcel.getSl();
            if(sl==null){
                throw new RuntimeException("第"+(i+1)+"行数量不可为空！");
            }
            Double dj = wwfysmxExcel.getDj();
            if(dj==null){
                throw new RuntimeException("第"+(i+1)+"行单价不可为空！");
            }
            Double xj = wwfysmxExcel.getXj();
            if (xj == null) {
                throw new RuntimeException("第"+(i+1)+"行小计不可为空！");
            }
            String bz = wwfysmxExcel.getBz();
//            ElsProjectReportExploit projectId = elsProjectReportExploitMapper.selectOne(new QueryWrapper<ElsProjectReportExploit>().eq("project_id", gouZhiId));
//            wwfysmxMapper.deleteByids(Long.valueOf(gouZhiId));
            ElsReportKaifaWwfysmx elsReportGouzhiJfys = new ElsReportKaifaWwfysmx();
            elsReportGouzhiJfys.setId(Long.valueOf(gouZhiId));
            elsReportGouzhiJfys.setGznr(gznr);
            elsReportGouzhiJfys.setSl(sl);
            elsReportGouzhiJfys.setDj(dj);
            elsReportGouzhiJfys.setXj(xj);
            elsReportGouzhiJfys.setBz(bz);
            wwfysmxMapper.insert(elsReportGouzhiJfys);
            list.add(elsReportGouzhiJfys);
        }
        return R.data(list);
    }

    @Override
    public List<ElsReportKaifaWwfysmx> getWwfysmxId(String id) {
        return wwfysmxMapper.selectList(new QueryWrapper<ElsReportKaifaWwfysmx>().eq("id",id));
    }

    @Override
    public Integer updateWwfysmx(List<ElsReportKaifaWwfysmx> wwfysmxes) {
        int insert = 0;
        wwfysmxMapper.deleteByids(wwfysmxes.get(0).getId());
        /*for (ElsReportKaifaWwfysmx wwfysmx : wwfysmxes) {
            insert = wwfysmxMapper.updateById(wwfysmx);
        }*/
        return insert;
    }

    @Override
    public Integer saveWwfysmx(ElsReportKaifaWwfysmx wwfysmxes) {
        Integer insert = wwfysmxMapper.insert(wwfysmxes);
        return insert;
    }
}
